package com.lottery.material.controller.admin.test;

import com.gitee.yongzhuzl.commonutil.util.http.HttpClientUtil;
import com.gitee.yongzhuzl.commonutil.util.json.JsonTool;
import com.lottery.material.util.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * description: IP测试Controller
 * @author 朱正磊
 * @date 2020-08-05 11:11:22
 */
@Controller
@RequestMapping("/ipTest")
public class IpTestController {

    private static final String APPKEY = "2551ee143ac5aa97";
    private static final String URL = "https://api.binstd.com/ip/location";

    /**
     * description: 根据ip查询所属地
     * @author 朱正磊
     * @date 2020-08-05 11:16:37
     * @param ips ip地址
     * @return 返回响应对象
     */
    @RequestMapping("/getIpAddress/{ips}")
    public ResponseResult getIpAddress(@PathVariable("ips") String ips) {
        List<String> ipList = Arrays.asList(ips.split(","));
        List<String> response = new ArrayList<>();
        for (String ip : ipList) {
            String requestUrl = URL + "?appkey=" + APPKEY + "&ip=" + ip;
            String result = HttpClientUtil.doGet(requestUrl);
            System.out.println(result);
            Map map = JsonTool.jsonToPojo(result, Map.class);
            Integer status = (Integer) map.get("status");
            if (status != 0) {
                String msg = (String) map.get("msg");
                System.out.println(msg);
            } else {
                LinkedHashMap responseMap = (LinkedHashMap) map.get("result");
                String queryIp = (String) responseMap.get("ip");
                String country = (String) responseMap.get("country");
                String province = (String) responseMap.get("province");
                String city = (String) responseMap.get("city");
                String type = (String) responseMap.get("type");
                String area = (String) responseMap.get("area");
                System.out.println(queryIp);
                System.out.println(country);
                System.out.println(province);
                System.out.println(city);
                System.out.println(type);
                System.out.println(area);
                response.add("ip地址：" + queryIp + "，所属地：" + area);
            }
        }
        return ResponseResult.buildSuccess(response);
    }

}
